# 引入django的请求库
from django.http import HttpResponse
# 引入渲染库
from django.shortcuts import render

# 用户发送请求
def home(request):
	# 返回一个字符串
	# return HttpResponse('hello world')
	# 返回一个html
	return render(request, "home.html")

# 统计结果
def count(request):
	# print(len(request.GET['text']))

	user_text = request.GET['text']

	word_dict = {}
	for word in user_text:
		if word not in word_dict:
			word_dict[word] = 1
		else:
			word_dict[word] += 1
	# 字典本身不能迭代 items转化字典				
	sorted_dict = sorted(word_dict.items(), key=lambda w:w[1], reverse=True)		

	# return HttpResponse('count')
	return render(request, "count.html", {
		'total_count': len(user_text)
		,'text': request.GET['text']
		,'word_dict': word_dict
		,'sorted_dict': sorted_dict
		} )

# 关于本网站
def about(request):
	return render(request, 'about.html')